﻿/********************************************************************************
** 作者： 吴汉阳
** 创建时间： 2009-5-21
** 描述：
** 用户管理-涉及到的相关方法
********************************************************************************/

var storeUser;       // 用户数据源
var gridUser;        // 用户Grid
var pageSize = 20;   // 每页显示数
var winChooseOrgan   // 选择部门窗体
var OrganID = '';
var OrganName = '';
var sectionTree;
var positionStore;
var UserID;
var isEdit = false;
var isPassWord = false;
var Password = "000000";

Ext.onReady(function() {
    Ext.QuickTips.init();
    // 本地加载图片代码
    Ext.BLANK_IMAGE_URL = strPath + 'extjs/resources/images/default/s.gif';

    positionStore = new Ext.data.SimpleStore({
        fields: ['Position', 'PositionName'],
        data: [['', '--请选择职务--'], [1, '投诉受理人员'], [2, '监察分局调查员'],
            [3, '综合室人员'], [4, '监察分局副局长'], [5, '监察分局局长'],
            [6, '法制股执法人员'], [7, '法制股领导']]
    });
    UserID = GetParastr("UserID");

    if (GetParastr("isEdit") == 'true') isEdit = true;
    if (GetParastr("isPassWord") == 'true') isPassWord = true;

    //    // 选择职位
    //    new Ext.form.ComboBox({
    //        hiddenName: 'Position',
    //        baseCls: "x-plain",
    //        id: 'cbPosition',
    //        store: positionStore,
    //        renderTo: 'divPosition',
    //        valueField: 'Position',
    //        selectOnFocus: true,
    //        typeAhead: true,
    //        width: 172,
    //        displayField: 'PositionName',
    //        mode: 'local',
    //        triggerAction: 'all',
    //        listWidth: 172,
    //        readOnly: true,
    //        editable: false
    //    })

    BindUserInfo(UserID);
});

// 部门树根节点
var rootNodeTrade = { nodeType: 'async', text: '部门列表', draggable: false, id: 'OrganizationTree', collapsed: false, expanded: true };
sectionTree = new Ext.tree.TreePanel({
    // title:'部门列表',
    animate: true,
    enableDD: true,
    enableDrag: true,
    enableDrop: true,
    rootVisible: true,
    autoScroll: true,
    useArrows: false,
    width: 360,
    height: 400,
    lines: true,
    root: rootNodeTrade,
    loader: new Ext.tree.TreeLoader(
        {
            dataUrl: 'GetOrganizationTree.aspx'
        })
});
// Tree的点击事件。
sectionTree.on('click', function(node) {
    if (node.id != "OrganizationTree" && node.id != "") {
        OrganID = node.id;
        OrganName = node.text;
    }
});
// Tree的点击事件。
sectionTree.on('dblclick', function(node) {
    if (node.id != "OrganizationTree" && node.id != "") {
        OrganID = node.id;
        OrganName = node.text;
        setValue('txtOrganName', OrganName);
        winChooseOrgan.hide();
    }
});

// 选择部门窗体 #开始 ---------------------------------------------------------------------------------------------------------------------------
winChooseOrgan = new Ext.Window({
    title: '选择部门',
    width: 320,
    height: 250,
    layout: 'fit',
    plain: true,
    closeAction: 'hide',
    modal: true,
    deferredRender: false,
    bodyStyle: 'padding:5px;',
    buttonAlign: 'center',
    items: sectionTree,
    buttons: [{
        text: '确定',
        id: 'btnSerachInfo',
        name: 'btnSerachInfo',
        handler: function() {
            setValue('txtOrganName', OrganName);
            winChooseOrgan.hide();
        }
    }, {
        text: '关闭',
        handler: function() {
            winChooseOrgan.hide();
        }
}]
    });

    // 绑定用户信息
    function BindUserInfo(UserID) {
        // 获取绑定信息
        controlShow("tdUserInfo");
        controlHide("tdUserPassWord");
        var organid = GetParastr("OrganID"); //获取URL部门ID
        var organname = GetParastr("OrganName"); //获URL传过来的部门名称
        if ((organid != null && organname != null) && (organid != '' && organname != '')) {
            OrganID = organid;
            OrganName = organname;
            Ext.get("txtOrganName").dom.value = OrganName;
        }
        if (isPassWord) {
            controlShow("tdUserPassWord");
            controlHide("tdUserInfo");
        }
        if (UserID.trim() != "")
            GetUserInfo(UserID);
    }
    // 获取用户信息
    function GetUserInfo(ID) {
        if (ID == null) return;
        Ext.get("txtUserNames").dom.disabled = true;
        var strUrls = strPath + 'GGFW/BaseInfo/FoothillUserCmd.fwf?Method=GetFoothillUser&UserID=' + ID;
        Ext.Ajax.request({
            url: strUrls,
            method: 'POST',
            failure: function(response, options) {
                ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
            },
            success: function(response, options) {
                // 绑定值
                var json = response.responseText;
                var obj = parseJsonResult(json);

                if (obj != null) {
                    if (!obj.Success) {
                        showMessage("错误！", "读取数据出错！", obj.Message);
                    }
                    else if (obj.data[0] != null) {
                        Ext.get("txtUserNames").dom.value = SiftNullAndEmpty(obj.data[0].UserName);
                        Ext.get("txtTrueNames").dom.value = SiftNullAndEmpty(obj.data[0].TrueName);
                        Ext.get("txtEdUserName").dom.value = SiftNullAndEmpty(obj.data[0].UserName);
                        Ext.get("txtEdTrueName").dom.value = SiftNullAndEmpty(obj.data[0].TrueName);
                        Ext.get("txtPhone").dom.value = SiftNullAndEmpty(obj.data[0].Phone);
                        Ext.get("txtMobilePhone").dom.value = SiftNullAndEmpty(obj.data[0].MobilePhone);
                        Ext.get("txtEmail").dom.value = SiftNullAndEmpty(obj.data[0].Email);
                        Ext.get("txtOrderID").dom.value = SiftNullAndEmpty(obj.data[0].OrderID);
                        Ext.get("txtRemark").dom.value = SiftNullAndEmpty(obj.data[0].Remark);
                        Ext.get("txtOrganName").dom.value = SiftNullAndEmpty(obj.data[0].OrganName);
                        OrganID = SiftNullAndEmpty(obj.data[0].OrganID);
                        Password = '';
                        // Ext.getCmp("cbPosition").setValue(obj.data[0].Position);
                        // 设置状态
                        var Status = SiftNullAndEmpty(obj.data[0].Status);
                        if (Status == '1') Ext.get("UserStatus").dom.checked = true;
                        else Ext.get("UserStatus").dom.checked = false;
                        // 设置性别
                        var Sex = SiftNullAndEmpty(obj.data[0].Sex);
                        if (Sex == '0') document.getElementsByName("chRadio")[1].checked = true;
                        else document.getElementsByName("chRadio")[0].checked = true;
                    }
                }
                else {
                    showMessage("错误！", "读取数据出错！");
                }
            }
        });
    }
    // 打开选择部门窗体
    function OpenOrganWin() {
        sectionTree.expandPath('/OrganizationTree/1', false);
        winChooseOrgan.show();
    }
    // 新增编辑用户
    function AddUsers() {
        var myMask = new Ext.LoadMask(Ext.getBody(), {
            msg: "正在加载数据..."
        });
        myMask.show();
        // 获取参数
        var UserName = Ext.get("txtUserNames").dom.value;
        var TrueName = Ext.get("txtTrueNames").dom.value;
        var Sex = 1;
        var obj = document.getElementsByName("chRadio");
        for (i = 0; i < obj.length; i++) {
            if (obj[i].checked) {
                Sex = obj[i].value;
            }
        }
        var Status = Ext.get("UserStatus").dom.checked;
        if (Status) Status = 1;
        else Status = 0;
        var MobilePhone = Ext.get("txtMobilePhone").dom.value;
        var Phone = Ext.get("txtPhone").dom.value;
        var Email = Ext.get("txtEmail").dom.value;
        var OrderID = Ext.get("txtOrderID").dom.value;
        var Remark = Ext.get("txtRemark").dom.value;
        //var Password1 = Ext.get("txtPassword1").dom.value;
        //var Password2 = Ext.get("txtPassword2").dom.value;
        //var Position = Ext.getCmp("cbPosition").getValue();

        // 验证必填项
        var msg = "";
        var msgLen = "";
        var strLen = "";
        if (UserName.trim() == "") msg += "登录账号 ";
        if (TrueName.trim() == "") msg += "用户姓名 ";
        if (OrganID == "") msg += "部门 ";
        //if (!isEdit) {
        //    if (Password1.trim() == "" || Password2.trim() == "") msg += " 密码 ";
        //    if (Password1 != Password2) msgLen = " 两次密码不一致！";
        //}
        if (isPassWord) {
            msg = "";
            Password1 = Ext.get("txtEdPassWord1").dom.value;
            Password2 = Ext.get("txtEdPassWord2").dom.value;
            Password = Password1;
            if (Password1.trim() == "" || Password2.trim() == "") msg += " 密码 ";
            if (Password1 != Password2) msgLen = " 两次密码不一致！";
        }
        if (GetStrLength(UserName) > 20) strLen += "登录账号 ";
        if (GetStrLength(TrueName) > 50) strLen += "用户姓名 ";
        if (GetStrLength(Phone) > 50) strLen += "电话 ";
        if (GetStrLength(MobilePhone) > 50) strLen += "手机 ";
        if (GetStrLength(Email) > 50) strLen += "邮箱 ";
        if (GetStrLength(Remark) > 50) strLen += "备注 "
        if (msg != "" || msgLen != "" || strLen != "") {
            myMask.hide();
            Ext.MessageBox.alert("&nbsp;提示", (msg == "" ? "" : (msg + "不能为空！")) + (msgLen == "" ? "" : ("<br/>" + msgLen)) + (strLen == "" ? "" : ("<br/>" + strLen + "输入字符过长!")));
            return;
        }
        //var strUrl = strPath + 'GGFW/BaseInfo/UserCmd.fwf?Method=AddFoothillUserData';
        var strUrl = strPath + 'GGFW/BaseInfo/FoothillUserCmd.fwf?Method=AddFoothillUserData';
        if (isEdit) strUrl = strPath + 'GGFW/BaseInfo/FoothillUserCmd.fwf?Method=UpdateFoothillUserData';
        Ext.Ajax.request({
            url: strUrl,
            method: 'POST',
            params: {
                OrganID: OrganID,
                UserID: UserID,
                UserName: UserName,
                TrueName: TrueName,
                Password: Password,
                Sex: Sex,
                Status: Status,
                MobilePhone: MobilePhone,
                Phone: Phone,
                Email: Email,
                // Position: Position,
                OrderID: OrderID,
                Remark: Remark
            },
            failure: function(response, options) {
                myMask.hide();
                ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
            },
            success: function(response, options) {
                myMask.hide();
                var json = response.responseText;
                var obj = parseJsonResult(json);

                if (!obj.Success) {
                    showMessage("错误！", "保存数据出错！", obj.Message);
                }
                else {
                    document.getElementById('btnClose').onclick();
                }
            }
        });
    }

    // 验证正整数
    function limitNum(obj) {
        obj.value = obj.value.replace(/\D/g, '');
        if (obj.value < 0) {
            Ext.get("txtOrderID").dom.value = 0;
        }
        else Ext.get("txtOrderID").dom.value = obj.value;
    }